<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of IndexController
 *
 * @author simon.zwicker
 */


class IndexController extends Zend_Controller_Action
{

    public function _init()
    {
        parent::init();
    }
    
    protected function getLoginForm()
    {
        $form = new Zend_Form;
        $form->setAction('/index/checklogin')->setMethod('post');
        
        $email = new Zend_Form_Element_Text('email',array('placeholder' => 'E-Mail Adresse'));
        $email->addValidator('alnum')
              ->addValidator('regex', false, array('/^[a-z]/'))
              ->setRequired(true)
              ->addFilter('StringToLower');

        $password = new Zend_Form_Element_Password('password',array('placeholder' => 'Passwort'));
        $password->setRequired(true)
                 ->addValidator('regex', false, array('/^[a-z]/'))
                 ->addFilter('StringToLower');
        
        $form->addElement($email)
             ->addElement($password)
             ->addElement('submit','login',array('label' => 'Login'));
        
        return $form;
    }
    
    protected function getRegisterForm()
    {
        $form = new Zend_Form;
        $form->setAction('/index/checkregister')->setMethod('post');
        
        $name = new Zend_Form_Element_Text('name',array('placeholder' => 'Name'));
        $firstname = new Zend_Form_Element_Text('firstname',array('placeholder' => 'Vorname'));
        $email = new Zend_Form_Element_Text('email',array('placeholder' => 'E-Mail Adresse'));
        $password = new Zend_Form_Element_Password('password',array('placeholder' => 'Passwort'));
        $password_wdh = new Zend_Form_Element_Password('password_wdh',array('placeholder' => 'Passwort wiederholen'));
        
        $form->addElement($name)
             ->addElement($firstname)
             ->addElement($email)
             ->addElement($password)
             ->addElement($password_wdh)
             ->addElement('submit','register',array('label' => 'Registrieren'));
        
        return $form;
    }
    
    public function indexAction()
    {
       $this->view->start = '<h1>Startseite</h1>';        
    }
    
    public function loginAction()
    {
        $this->view->form = $this->getLoginForm();
    }
       
    public function registerAction()
    {
        $this->view->form = $this->getRegisterForm();
    }
    
    public function checkloginAction()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        //Passwortverschlüsselung
        $shapass = sha1(md5(sha1($this->getRequest()->getPost('email').':'.$this->getRequest()->getPost('password'))));
        
        //Funktion
        $sql = 'SELECT * FROM user WHERE email = "' . $this->getRequest()->getPost('email') . '" AND passwort = "' . $shapass . '"';
        $data = $db->fetchAll($sql);
        if ($data) {
            //StandardSessionVariablen
            $_SESSION['logged'] = true;
            $_SESSION['admin'] = $data[0]['admin'];
            $_SESSION['user_id'] = $data[0]['user_id'];
            $_SESSION['name'] = $data[0]['name'];
            $_SESSION['vorname'] = $data[0]['vorname'];
            $_SESSION['email'] = $this->getRequest()->getPost('email');
            
            //SessionVariable Firma
            $sql = 'SELECT user.user_id, userfirma.firma_id, userfirma.status,adresse.name,firma.create_id FROM user,userfirma,adresse,firma '
                 . 'WHERE userfirma.user_id = '.$_SESSION['user_id'].' AND userfirma.firma_id = firma.firma_id AND firma.address_id = adresse.address_id';
            $data = $db->fetchAll($sql);
            if ($data) {
                if ($data[0]['status'] == 0) {
                    $_SESSION['firma'] = $data[0]['name'];
                } else {
                    $_SESSION['firma'] = 'Keine';
                }
            } else {
                $_SESSION['companyshow'] = 1;
                $_SESSION['firma'] = 'Keine';
            }
            
            //SessionVariable Gründer
            if ($_SESSION['user_id'] == $data[0]['create_id']) {
                $_SESSION['my_firma'] = $data[0]['firma_id'];
            }
            
            $sql = 'SELECT * FROM userfirma WHERE user_id = "' . $_SESSION['user_id'] . '"';
            $data = $db->fetchAll($sql);
            if ($data) {
                if ($data[0]['status'] == 1) {
                    $_SESSION['load'] = 1;
                }
                $sql = 'SELECT a.name FROM adresse a,firma f,userfirma uf '
                 . 'WHERE a.address_id = f.address_id AND f.firma_id = uf.firma_id AND uf.user_id = '. $_SESSION['user_id'];
                $data = $db->fetchAll($sql);
                if ($data) {
                    $_SESSION['loadname'] = $data[0]['name'];
                }
            }
            
            //Session Variable Essen
            date_default_timezone_set('Europe/Paris');
            $userdate = md5(date('d.m.Y').':'.$_SESSION['user_id']);
            $data = $db->fetchAll('SELECT * FROM essen WHERE user_date = "'. $userdate .'"');
            if ($data) {
                $_SESSION['bestellung'] = TRUE;
                $_SESSION['bestellungE'] = $data[0]['essen'];
                $data = $db->fetchAll('SELECT a.name FROM caterer c,adresse a,essen e WHERE c.address_id = a.address_id AND c.caterer_id = e.caterer_id AND e.user_date ="'. $userdate .'"');
                if ($data) {
                    $_SESSION['bestellungC'] = $data[0]['name'];
                }
            }            
            
            header('location: /index/index');
        } else {
            $this->view->logerror = '<h1>LOGINFEHLER</h1>
                 Benutzerdaten wurden falsch eingegeben oder sind nicht vorhanden!<br /><br /><a href="javascript:back()">Noch einmal probieren</a>';
        }
    }
    
    public function logoutAction()
    {
        session_destroy();
        header('location: /index/index');
    }
    
    public function checkregisterAction()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        $reg_param = $this->getRequest()->getParams();
        if (!empty($reg_param['name']) && !empty($reg_param['firstname']) && !empty($reg_param['email']) && !empty($reg_param['password']) && !empty($reg_param['password_wdh'])) {
            if ($reg_param['password'] == $reg_param['password_wdh']) {
                $existing = $db->fetchOne('SELECT * FROM user WHERE email = "' . $reg_param['email'] . '"');
                if (!$existing) {
                    ($db->insert('user', array(
                        'name' => $reg_param['name'],
                        'vorname' => $reg_param['firstname'],
                        'email' => $reg_param['email'],
                        'passwort' => sha1(md5(sha1($reg_param['email'].':'.$reg_param['password'])))
                    )));
                    
                    $this->view->msgreg = 'Der Benutzer wurde angelegt! <br /> <br /> <a href="/index/login">Zum Login</a>';
                } else {
                    $this->view->msgreg =  'Dieser Benutzer ist bereits vorhanden. <br /><br /><a href="/index/forget">Passwort vergessen?</a>
                        <br /><br /><a href="/index/login">Login</a>';
                }
            } else {
                $this->view->msgreg =  'Passwörter stimmen nicht überein!<br /><br /><a href="javascript:back()">zur Registrierung</a>';
            }
        } else {
            $this->view->msgreg =  'Bitte alle Felder ausfüllen!<br /><br /><a style="hover: black" href="javascript:back()">Eingaben ergänzen</a>';
        }
    }
    
}
    
    